const tree = require('./tree');

// 先序遍历
const preOrderTraverseNode = (root) => {
	const res = [];
	const traverse = (node) => {
		if (node != null) {
			res.push(node.val);
			traverse(node.left);
			traverse(node.right);
		}
	}
	traverse(root);
	return res;
}

// 中序遍历
const inOrderTraverseNode = (root) => {
	const res = [];
	const traverse = (node) => {
		if (node != null) {
			traverse(node.left);
			res.push(node.val);
			traverse(node.right);
		}
	}
	traverse(root);
	return res;
}

// 后序遍历
const postOrderTraverseNode = (root) => {
	const res = [];
	const traverse = (node) => {
		if (node != null) {
			traverse(node.left);
			traverse(node.right);
			res.push(node.val);
		}
	}
	traverse(root);
	return res;
}

const preOrderTraverseResult = preOrderTraverseNode(tree);
const inOrderTraverseResult = inOrderTraverseNode(tree);
const postOrderTraverseResult = postOrderTraverseNode(tree);

console.log(preOrderTraverseResult); // [ 11, 7, 5, 3, 9, 8, 10, 15, 13, 12, 14, 20, 18, 25 ]
console.log(inOrderTraverseResult); // [ 3, 5, 7, 8, 9, 10, 11, 12, 13, 14, 15, 18, 20, 25 ]
console.log(postOrderTraverseResult); // [ 3, 5, 8, 10, 9, 7, 12, 14, 13, 18, 25, 20, 15, 11 ]
